package com.hsmpay.common.action;

import com.hsmpay.common.util.system.Constant;
import com.opensymphony.xwork2.ActionSupport;
import com.hsmpay.common.util.JSONStringBuilder;
import net.sf.json.JSONArray;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * anction 基类
 *
 * @author 颜铃璋
 *         建立时间：2012-11-22
 */
public class BaseAction extends ActionSupport {
    private static final long serialVersionUID = 2109773313593099908L;
    private static Logger log = Logger.getLogger(BaseAction.class);
    private HttpServletResponse response;

    /**
     * 获得request
     *
     * @return
     */
    public HttpServletRequest getRequest() {
        return ServletActionContext.getRequest();
    }

    /**
     * 获得response
     *
     * @return
     */
    public HttpServletResponse getResponse() {
        if (null == response) {
            response = ServletActionContext.getResponse();
        }
        return response;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    /**
     * 获得session
     *
     * @return
     */
    public HttpSession getSession() {
        return getRequest().getSession();
    }

    /**
     * 获得servlet上下文
     *
     * @return
     */
    public ServletContext getServletContext() {
        return ServletActionContext.getServletContext();
    }

    /**
     * 获得真实路径
     *
     * @param path
     * @return
     */
    public String getRealPath(String path) {
        return Constant.rootPath+"/"+path;
        /*
        String classPath = this.getClass().getClassLoader().getResource("").getPath();
        String basePath = classPath.substring(0, classPath.indexOf("WEB-INF")) + path;
        return basePath;
        */
    }

    /**
     * 返回Ajax响应字符串，将处理结果返回
     */
    public void sendAjaxResponse(String message) {
        try {
            HttpServletResponse res = ServletActionContext.getResponse();
            res.setCharacterEncoding("UTF-8");
            res.setContentType("text/json");
            log.debug("-------------------响应String数据---------------------\n" + message + "\n----------------------------------------------");
            //getResponse().addHeader("Content-Length", message.getBytes("UTF-8").length+"");
            res.getWriter().print(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 返回Ajax响应字符串，将处理结果返回
     */
    protected void sendAjaxResponse(Object obj) {
        sendAjaxResponse(JSONStringBuilder.getAjaxString(obj));
    }

    /**
     * 返回Ajax响应字符串，将处理结果返回
     *
     * @param list 查询数据
     * @throws Exception 异常信息
     */
    @SuppressWarnings("unchecked")
    protected void sendAjaxResponse(List list) {
        sendAjaxResponse(JSONArray.fromObject(list).toString());
    }
}
